//
//  LoginViewController.m
//  DuoDian
//
//  Created by 张卓 on 2017/8/16.
//  Copyright © 2017年 bigcastcloudShop. All rights reserved.
//

#import "LoginViewController.h"
#import "mobileIsTrue.h"
#import "HomeViewController.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *passWord;
@property (weak, nonatomic) IBOutlet UIButton *achive;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation LoginViewController
/// 获取验证码
- (IBAction)achiveClickBtn:(id)sender {
    
    if([self.userName.text isEqualToString:@""]){
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.mode = MBProgressHUDModeCustomView;
        UIImage *image = [[UIImage imageNamed:@"wa.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        hud.customView = [[UIImageView alloc] initWithImage:image];
        hud.square = YES;
        hud.label.text = NSLocalizedString(@"请输入手机号码", @"HUD done title");
        [hud hideAnimated:YES afterDelay:1.f];
        return;
    }
    NSString *mobStr = self.userName.text;
    if(![mobileIsTrue isMobileNumber:mobStr]){
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.mode = MBProgressHUDModeCustomView;
        UIImage *image = [[UIImage imageNamed:@"wa.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        hud.customView = [[UIImageView alloc] initWithImage:image];
        hud.square = YES;
        hud.label.text = NSLocalizedString(@"请输入正确的手机号码！", @"HUD done title");
        [hud hideAnimated:YES afterDelay:1.f];
        return;
    }else{
        // 解决按钮短时间内多次点击只触发一次事件方法
        [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(startTime2) object:sender];
        [self performSelector:@selector(startTime2) withObject:sender afterDelay:0.2f];
        
        
        
        
        
        
        
        
        
    }

}
- (IBAction)loginClickBtn:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.loginBtn.layer.masksToBounds = YES;
    self.loginBtn.layer.cornerRadius = 5;
    self.navigationItem.leftBarButtonItem = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
// 验证码倒计时
- (void)startTime2 {
    __block int timeout = 120; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0); //每秒执行
    WeakSelf(weakSelf)
    dispatch_source_set_event_handler(_timer, ^{
        StrongSelf(strongSelf)
        if (timeout <= 0) { //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [strongSelf.achive setTitle:@"重新发送" forState:UIControlStateNormal];
                strongSelf.achive.userInteractionEnabled = YES;
            });
        } else {
            NSString *strTime = [NSString stringWithFormat:@"%.ds", timeout];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                [strongSelf.achive setTitle:[NSString stringWithFormat:@"%@",strTime] forState:UIControlStateNormal];
                [UIView commitAnimations];
                strongSelf.achive.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
